/**
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates.
 * The Universal Permissive License (UPL), Version 1.0
 */
"use strict";
define(["ojs/ojcore","jquery","ojs/ojdatasource-common","ojs/ojmodel"],function(a){a.le=function(g,b,c,d,e){a.p.jF(c);this.fd=g;this.pg=b;this.FN=c;this.qa=d;this.Qd=e};o_("CollectionHeaderSet",a.le,a);a.le.prototype.Wja=function(g){a.p.hM(g);null!=g&&g.length===this.getCount()&&(this.Xw=g)};a.le.prototype.getData=function(g,b){var c;a.p.assert(g<=this.pg&&g>=this.fd,"index out of bounds");a.p.assert(null==b||0==b,"level out of bounds");if(null!=this.qa){if(null==this.Xw)return null;c=this.Xw[g-this.fd];
return c.get(this.qa)}return this.FN[g]};a.b.g("CollectionHeaderSet.prototype.getData",{getData:a.le.prototype.getData});a.le.prototype.getMetadata=function(g,b){var c;a.p.assert(g<=this.pg&&g>=this.fd,"index out of bounds");a.p.assert(null==b||0==b,"level out of bounds");if(null!=this.qa){if(null==this.Xw)return null;c=this.Xw[g-this.fd];return{key:a.pu.Ty(c)}}c=this.getData(g,b);return this.Qd.key===c?{key:c,sortDirection:this.Qd.direction}:{key:c}};a.b.g("CollectionHeaderSet.prototype.getMetadata",
{getMetadata:a.le.prototype.getMetadata});a.le.prototype.getLevelCount=function(){return 0<this.getCount()?1:0};a.b.g("CollectionHeaderSet.prototype.getLevelCount",{getLevelCount:a.le.prototype.getLevelCount});a.le.prototype.getExtent=function(g,b){a.p.assert(g<=this.pg&&g>=this.fd,"index out of bounds");a.p.assert(null==b||0==b,"level out of bounds");return{extent:1,more:{before:!1,after:!1}}};a.b.g("CollectionHeaderSet.prototype.getExtent",{getExtent:a.le.prototype.getExtent});a.le.prototype.getDepth=
function(g,b){a.p.assert(g<=this.pg&&g>=this.fd,"index out of bounds");a.p.assert(null==b||0==b,"level out of bounds");return 1};a.b.g("CollectionHeaderSet.prototype.getDepth",{getDepth:a.le.prototype.getDepth});a.le.prototype.getCount=function(){return Math.max(0,this.pg-this.fd)};a.b.g("CollectionHeaderSet.prototype.getCount",{getCount:a.le.prototype.getCount});a.le.prototype.getStart=function(){return this.fd};a.b.g("CollectionHeaderSet.prototype.getStart",{getStart:a.le.prototype.getStart});a.le.prototype.qga=
function(){return this.pg};a.b.g("CollectionHeaderSet.prototype.getEnd",{qga:a.le.prototype.qga});a.le.prototype.wga=function(){return this.FN};a.b.g("CollectionHeaderSet.prototype.getHeaders",{wga:a.le.prototype.wga});a.le.prototype.yF=function(){return this.qa};a.b.g("CollectionHeaderSet.prototype.getRowHeader",{yF:a.le.prototype.yF});a.Ff=function(g,b,c,d,e){a.p.jF(e);this.Ea=g;this.ob=b;this.Mp=c;this.Uw=d;this.Tf=e};o_("CollectionCellSet",a.Ff,a);a.Ff.prototype.Wja=function(g){a.p.hM(g);null!=
g&&g.length===this.getCount("row")&&(this.Xw=g)};a.Ff.prototype.getData=function(a){var b,c;c=this.Rc(a);if(null==c)return null;b=this.Tf[a.column];a={};Object.defineProperty(a,"data",{get:function(){return c.get(b)},set:function(a){c.set(b,a,{silent:!0})}});return a};a.b.g("CollectionCellSet.prototype.getData",{getData:a.Ff.prototype.getData});a.Ff.prototype.getMetadata=function(g){var b;b=this.Rc(g);return null==b?null:{keys:{row:a.pu.Ty(b),column:this.Tf[g.column]}}};a.b.g("CollectionCellSet.prototype.getMetadata",
{getMetadata:a.Ff.prototype.getMetadata});a.Ff.prototype.Rc=function(g){var b;if(null==this.Xw)return null;a.p.mi(g);b=g.row;g=g.column;a.p.assert(b>=this.Ea&&b<=this.ob&&g>=this.Mp&&g<=this.Uw);return this.Xw[b-this.Ea]};a.Ff.prototype.getCount=function(a){return"row"===a?Math.max(0,this.ob-this.Ea):"column"===a?Math.max(0,this.Uw-this.Mp):0};a.b.g("CollectionCellSet.prototype.getCount",{getCount:a.Ff.prototype.getCount});a.Ff.prototype.Ap=function(){return this.Ea};a.b.g("CollectionCellSet.prototype.getStartRow",
{Ap:a.Ff.prototype.Ap});a.Ff.prototype.sga=function(){return this.ob};a.b.g("CollectionCellSet.prototype.getEndRow",{sga:a.Ff.prototype.sga});a.Ff.prototype.zp=function(){return this.Mp};a.b.g("CollectionCellSet.prototype.getStartColumn",{zp:a.Ff.prototype.zp});a.Ff.prototype.rga=function(){return this.Uw};a.b.g("CollectionCellSet.prototype.getEndColumn",{rga:a.Ff.prototype.rga});a.Ff.prototype.Bt=function(){return this.Tf};a.b.g("CollectionCellSet.prototype.getColumns",{Bt:a.Ff.prototype.Bt});a.pu=
function(){};a.pu.Ty=function(a){var b;b=a.Mi();null==b&&(b=a.zu());return b};a.Va=function(g,b){this.fb=g;null!=b&&(this.uG=b.rowHeader,this.columns=b.columns);this.qda();a.Va.u.constructor.call(this)};o_("CollectionDataGridDataSource",a.Va,a);a.b.sa(a.Va,a.Rr,"oj.CollectionDataGridDataSource");a.Va.prototype.Init=function(){a.Va.u.Init.call(this);this.Fr={};this.AE()};a.b.g("CollectionDataGridDataSource.prototype.Init",{Init:a.Va.prototype.Init});a.Va.prototype.AE=function(){this.fb.on("add",this.yxa.bind(this));
this.fb.on("remove",this.Cxa.bind(this));this.fb.on("change",this.zxa.bind(this));this.fb.on("refresh",this.wwa.bind(this));this.fb.on("reset",this.xwa.bind(this))};a.Va.prototype.oza=function(){return null!=this.data};a.Va.prototype.getCount=function(a){var b;void 0==this.precision&&(this.precision={});if("row"==a){b=this.Pf();if(-1===b||0===b&&(!this.oza()||0<this.jt()))return this.precision[a]="estimate",-1;this.precision[a]="exact";return this.jt()}if("column"==a){if(null!=this.columns)return this.precision[a]=
"exact",this.columns.length;this.precision[a]="estimate";return-1}return 0};a.b.g("CollectionDataGridDataSource.prototype.getCount",{getCount:a.Va.prototype.getCount});a.Va.prototype.getCountPrecision=function(a){null!=this.precision&&null!=this.precision[a]||this.getCount(a);return this.precision[a]};a.b.g("CollectionDataGridDataSource.prototype.getCountPrecision",{getCountPrecision:a.Va.prototype.getCountPrecision});a.Va.prototype.fetchHeaders=function(a,b,c){var d,e;null!=b&&(d=a.axis,e={},e.Dha=
a,e.callbacks=b,e.kF=c,this.Fr[d]=e)};a.b.g("CollectionDataGridDataSource.prototype.fetchHeaders",{fetchHeaders:a.Va.prototype.fetchHeaders});a.Va.prototype.ixa=function(g,b,c,d){var e,f,h,k;e=g.axis;f=g.start;h=g.count;if("column"===e)null!=this.columns&&(d=Math.min(this.columns.length,f+h),k=new a.le(f,d,this.columns,void 0,this.NE));else if("row"===e&&null!=this.uG){null!=d&&(h=d.count);d=Math.min(this.jt(),f+h);k=new a.le(f,d,this.columns,this.uG);this.xca(f,d,k,g,b,c);return}null!=b&&b.success&&
b.success.call(c.success,k,g,null)};a.Va.prototype.H8=function(a){var b,c,d,e,f,h;for(b=0;b<a.length;b+=1)c=a[b],"row"===c.axis?(d=c.start,e=c.count):"column"===c.axis&&(f=c.start,h=c.count);return{rowStart:d,rowCount:e,colStart:f,colCount:h}};a.Va.prototype.owa=function(g,b,c,d){var e,f,h;e=this.H8(g);f=e.rowStart;d=null!=d?Math.min(this.jt(),f+d.count):Math.min(this.jt(),f+e.rowCount);h=e.colStart;e=Math.min(null==this.columns?0:this.columns.length,h+e.colCount);e=new a.Ff(f,d,h,e,this.columns);
this.xca(f,d,e,g,b,c)};a.Va.prototype.xca=function(a,b,c,d,e,f){var h;for(h=[];a<b;a++)h.push(this.fb.at(a,{deferred:!0}));Promise.all(h).then(function(a){c.Wja(a);e.success.call(f.success,c,d)})};a.Va.prototype.fetchCells=function(a,b,c){null!=b&&(this.Ci={},this.Ci.dY=a,this.Ci.callbacks=b,this.Ci.kF=c);this.Mta(a)};a.b.g("CollectionDataGridDataSource.prototype.fetchCells",{fetchCells:a.Va.prototype.fetchCells});a.Va.prototype.uba=function(a){var b,c,d,e,f;b=this.Fr[a];null!=b&&(c=b.Dha,d=b.callbacks,
e=b.kF,"row"===a&&(f=b.OX),this.ixa(c,d,e,f),this.Fr[a]=null)};a.Va.prototype.BBa=function(){this.owa(this.Ci.dY,this.Ci.callbacks,this.Ci.kF,this.Ci.OX)};a.Va.prototype.Mta=function(a){var b,c;b=this.H8(a);c=b.rowStart;this.fb.vG(c,b.rowCount).then(function(b){this.data=!0;this.uDa(b.start,b.count);void 0===this.columns?this.fb.at(c,{deferred:!0}).then(function(b){null!=b&&this.MW(b);this.V6(a)}.bind(this)):this.V6(a)}.bind(this),function(a){this.Nta(a)}.bind(this))};a.Va.prototype.Nta=function(g){a.t.error(g);
null!=this.Fr&&(this.vba("column",g),this.vba("row",g));null!=this.Ci&&this.CBa(g)};a.Va.prototype.vba=function(a,b){var c,d,e;c=this.Fr[a];null!=c&&(d=c.callbacks,e=c.kF,c=c.Dha,d.error&&d.error.call(e.error,b,c),this.Fr[a]=null)};a.Va.prototype.CBa=function(a){var b,c,d;b=this.Ci.callbacks;c=this.Ci.kF;d=this.Ci.dY;b.error&&b.error.call(c.error,a,d);this.Ci=null};a.Va.prototype.V6=function(a){this.Ci.dY==a&&(null!=this.Fr&&(this.uba("column"),this.uba("row")),null!=this.Ci&&this.BBa())};a.Va.prototype.uDa=
function(a,b){var c={start:a,count:b};null!=this.Fr.row&&(this.Fr.row.OX=c);null!=this.Ci&&(this.Ci.OX=c)};a.Va.prototype.MW=function(a){this.columns=a.keys();-1!=this.columns.indexOf(this.uG)&&this.columns.splice(this.columns.indexOf(this.uG),1)};a.Va.prototype.keys=function(g){var b,c,d,e,f;b=g.row;c=g.column;f=this;return new Promise(function(g){f.fb.at(b,{deferred:!0}).then(function(b){null==b?g({row:null,column:null}):(d=a.pu.Ty(b),null==f.columns&&f.MW(b),e=f.columns[c],g({row:d,column:e}))}.bind(f))})};
a.b.g("CollectionDataGridDataSource.prototype.keys",{keys:a.Va.prototype.keys});a.Va.prototype.indexes=function(a){var b,c,d,e;b=a.row;c=a.column;e=this;return new Promise(function(a){e.fb.indexOf(b,{deferred:!0}).then(function(b){-1===b?a({row:-1,column:-1}):null==e.columns?e.fb.at(b,{deferred:!0}).then(function(g){e.MW(g);d=e.columns.indexOf(c);-1===d&&(b=-1);a({row:b,column:d})}.bind(e)):(d=e.columns.indexOf(c),-1===d&&(b=-1),a({row:b,column:d}))}.bind(e))})};a.b.g("CollectionDataGridDataSource.prototype.indexes",
{indexes:a.Va.prototype.indexes});a.Va.prototype.getCapability=function(a){return"sort"===a?"column":"move"===a?"row":null};a.b.g("CollectionDataGridDataSource.prototype.getCapability",{getCapability:a.Va.prototype.getCapability});a.Va.prototype.sort=function(a,b,c){var d,e,f;null==c&&(c={});null==a?this.tW(b,c):(e=a.direction,f=a.key,a=a.axis,"column"===a?(this.fb.Ib()?(this.fb.comparator=f,this.fb.sortDirection="ascending"===e?1:-1):("ascending"===e&&(d=function(a,b){var c,d;a=a.get(f);b=b.get(f);
c=isNaN(a);d=isNaN(b);a instanceof Date&&(a=a.toISOString(),c=!0);b instanceof Date&&(b=b.toISOString(),d=!0);return c&&d?a<b?-1:a===b?0:1:c?1:d?-1:a-b}),"descending"===e&&(d=function(a,b){var c,d;a=a.get(f);b=b.get(f);c=isNaN(a);d=isNaN(b);a instanceof Date&&(a=a.toISOString());b instanceof Date&&(b=b.toISOString());return c&&d?a>b?-1:a===b?0:1:c?-1:d?1:b-a}),this.fb.comparator=d),this.fb.sort(),this.qda(f),null!=b&&null!=b.success&&b.success.call(c.success)):null!=b&&null!=b.error&&b.error.call(c.error,
"Axis value not supported"))};a.b.g("CollectionDataGridDataSource.prototype.sort",{sort:a.Va.prototype.sort});a.Va.prototype.tW=function(a,b){this.fb.comparator=null;this.fb.reset();null!=a&&null!=a.success&&a.success.call(b.success)};a.Va.prototype.qda=function(a){var b,c;b=this.fb.comparator;c=-1===this.fb.sortDirection?"descending":"ascending";null==a&&"function"===typeof b?this.NE={}:(this.NE={},this.NE.axis="column",this.NE.direction=c,this.NE.key=null==a?b:null)};a.Va.prototype.move=function(a,
b,c,d,e){var f;this.fb.get(a,{deferred:!0}).then(function(c){null==b?(this.fb.remove(c),this.fb.add(c),null!=d&&null!=d.success&&d.success.call(e.success)):(a===b?(f=this.fb.indexOf(b,{deferred:!0}),this.fb.remove(c)):(this.fb.remove(c),f=this.fb.indexOf(b,{deferred:!0})),f.then(function(a){this.fb.add(c,{at:a,LNa:!0});null!=d&&null!=d.success&&d.success.call(e.success)}.bind(this)))}.bind(this))};a.b.g("CollectionDataGridDataSource.prototype.move",{move:a.Va.prototype.move});a.Va.prototype.moveOK=
function(){return"valid"};a.b.g("CollectionDataGridDataSource.prototype.moveOK",{moveOK:a.Va.prototype.moveOK});a.Va.prototype.Io=function(a,b,c,d,e){var f={source:this};f.operation=a;f.keys={row:b,column:c};f.indexes={row:d,column:e};return f};a.Va.prototype.yxa=function(g){this.handleEvent("change",this.Io("insert",a.pu.Ty(g),null,g.index,-1))};a.Va.prototype.Cxa=function(g,b,c){this.handleEvent("change",this.Io("delete",a.pu.Ty(g),null,c.index,-1))};a.Va.prototype.zxa=function(g){this.handleEvent("change",
this.Io("update",a.pu.Ty(g),null,g.index,-1))};a.Va.prototype.wwa=function(){this.data=null;this.handleEvent("change",this.Io("refresh",null,null))};a.Va.prototype.xwa=function(){this.data=null;this.handleEvent("change",this.Io("reset",null,null))};a.Va.prototype.jt=function(){return this.fb.size()};a.Va.prototype.Pf=function(){return void 0===this.fb.totalResults?-1:this.fb.totalResults};a.Va.prototype.hga=function(){return this.fb};a.b.g("CollectionDataGridDataSource.prototype.getCollection",{hga:a.Va.prototype.hga});
a.Va.prototype.Bt=function(){return this.columns};a.b.g("CollectionDataGridDataSource.prototype.getColumns",{Bt:a.Va.prototype.Bt});a.Va.prototype.yF=function(){return this.uG};a.b.g("CollectionDataGridDataSource.prototype.getRowHeader",{yF:a.Va.prototype.yF});a.Va.prototype.getData=function(){return this.data};a.b.g("CollectionDataGridDataSource.prototype.getData",{getData:a.Va.prototype.getData})});